/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package example;
import facecat.topin.core.*;
import facecat.topin.btn.*;

/*
* 复选框扩展
 */
public class MyCheckBox extends FCCheckBox {
    /*
    * 创建复选框
     */
    public MyCheckBox()
    {
        m_buttonSize = new FCSize(30, 14);
        m_borderColor = FCColor.None;
    }

    /*
    * 当前的横向动画位置
     */
    private int m_currentX = -1;

    /*
    * 秒表ID
     */
    private int m_timerID = FCView.getNewTimerID();

    /*
    * 点击事件
    * @param touchInfo 触摸信息
     */
    public void onClick(FCTouchInfo touchInfo)
    {
        boolean oldChecked = isChecked();
        super.onClick(touchInfo);
        boolean newChecked = isChecked();
        if (oldChecked != newChecked)
        {
            if (oldChecked)
            {
                m_currentX = (getButtonSize().cx - 22);
                startTimer(m_timerID, 20);

            }
            else
            {
                m_currentX = -(getButtonSize().cx - 22);
                startTimer(m_timerID, 20);
            }
        }
    }

    /*
     * 重绘方法
     * @param paint 绘图对象
     * @param clipRect 裁剪区域
     */
    public void onPaintCheckButton(FCPaint paint, FCRect clipRect)
    {
        FCRect drawRect = clipRect;
        int width = getWidth(), height = getHeight();
        boolean iChecked = isChecked();
        int d = getButtonSize().cy;
        int pHeight = 10;
        int round = 4;
        FCRect pRect = new FCRect(1, height / 2 - pHeight / 2, clipRect.right - 1, height / 2 + pHeight / 2);
        long backColor = FCColor.rgba(0, 0, 0, 25); ;
        long backColor1 = FCColor.rgb(150, 150, 150);
        FCRect ellipseRect = new FCRect(1, (height - d) / 2, 1 + d, (height + d) / 2);
        if (iChecked)
        {
            backColor = FCColor.rgba(42, 138, 195, 200);
            backColor1 = FCColor.rgb(42, 138, 195);
            ellipseRect.left = clipRect.right - 1 - d;
            ellipseRect.right = clipRect.right - 1;
        }
        if (m_currentX != -1)
        {
            ellipseRect.left += m_currentX;
            ellipseRect.right += m_currentX;
        }
        paint.fillRoundRect(backColor, pRect, round);
        paint.fillEllipse(backColor1, ellipseRect);
    }

    /*
     * 重绘边线方法
     * @param paint 绘图对象
     * @param clipRect 裁剪区域
     */
    public void onPaintBorder(FCPaint paint, FCRect clipRect)
    {
    }

    /*
     * 移除视图方法
     */
    public void onRemove()
    {
        super.onRemove();
        stopTimer(m_timerID);
    }

    /*
     * 秒表方法
     * @param timerID 秒表ID
     * @param clipRect 裁剪区域
     */
    public void onTimer(int timerID)
    {
        super.onTimer(timerID);
        if (m_timerID == timerID)
        {
            int aimLeft = 0;
            if (m_currentX != -1)
            {
                int xSub = aimLeft - m_currentX;
                if (xSub != 0)
                {
                    if (xSub > 0)
                    {
                        m_currentX += 2;
                    }
                    else if (xSub < 0)
                    {
                        m_currentX -= 2;
                    }
                    if (Math.abs(aimLeft - m_currentX) < 3)
                    {
                        m_currentX = aimLeft;
                        stopTimer(m_timerID);
                    }
                    invalidate();
                }
            }
        }
    }

    /*
     * 重绘前景方法
     * @param paint 绘图对象
     * @param clipRect 裁剪区域
     */
    public void onPaintForeground(FCPaint paint, FCRect clipRect)
    {
        String text = getText();
        int width = getWidth(), height = getHeight();
        if (width > 0 && height > 0)
        {
            FCRect buttonRect = new FCRect(5, (height - m_buttonSize.cy) / 2, 5 + m_buttonSize.cx, (height + m_buttonSize.cy) / 2);
            //绘制背景图
            onPaintCheckButton(paint, buttonRect);
            //绘制文字
            if (text != null && text.length() > 0)
            {
                FCFont font = getFont();
                FCSize tSize = paint.textSize(text, font, -1);
                FCPoint tLocation = new FCPoint(buttonRect.right + 5, (height - tSize.cy) / 2);
                FCRect tRect = new FCRect(tLocation.x, tLocation.y, tLocation.x + tSize.cx, tLocation.y + tSize.cy);
                long textColor = getPaintingTextColor();
                paint.drawText(text, textColor, font, tRect, -1);
            }
        }
    }
}
